#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>

castlemq::BindingManager::ptr bmp;

class BindingTest:public testing::Environment
{
    public:
        virtual void SetUp() override
        {
            bmp=std::make_shared<castlemq::BindingManager>("./data/meta1.db");
        }

        virtual void TearDown() override
        {
            //emp->clear();
        }
};

TEST(BindingTest,bind_test)
{
    bmp->bind("exchange1","queue1","music1",true);
    bmp->bind("exchange1","queue2","music2",true);
    bmp->bind("exchange2","queue3","music3",true);
    bmp->bind("exchange3","queue4","music4",true);
    ASSERT_EQ(bmp->size(),4);
}

TEST(BindingTest,select_test)
{
    ASSERT_EQ(bmp->exists("exchange1","queue1"),true);
    ASSERT_EQ(bmp->exists("exchange1","queue2"),true);
    ASSERT_EQ(bmp->exists("exchange2","queue3"),true);
    ASSERT_EQ(bmp->exists("exchange3","queue4"),true);
    castlemq::Binding::ptr bp=bmp->getBinding("exchange2","queue3");
    ASSERT_EQ(bp->exchange_name,"exchange2");
    ASSERT_EQ(bp->msg_queue_name,"queue3");
    ASSERT_EQ(bp->binding_key,"music3");
    castlemq::MsgQueueBindingMap mqmp= bmp->getExchangeBindings("exchange1");
}

TEST(BindingTest,remove_test)
{
    bmp->unbind("exchange1","queue2");
    ASSERT_EQ(bmp->exists("exchange1","queue2"),false);
    bmp->bind("exchange1","queue6","hello",true);
    bmp->bind("exchange3","queue3","hello",true);
    bmp->bind("exchange1","queue7","hello",true);
    ASSERT_EQ(bmp->size(),6);

    bmp->removeExchangeBindings("exchange1");
    bmp->removeMsgQueueBindings("queue3");
}


int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    RUN_ALL_TESTS();
    return 0;
}
